﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.BOL;
using System.Data;
using BYFW.SFL;
using LDSP.Helper;

namespace LDSP.UI.Admin
{
    public partial class AddAutoLoginAuthentication : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.CheckQS();
            }

            if (this.RB())
            {
                this.InitDataDynamicBefore();
                if (!IsPostBack)
                {
                    this.InitDataSelect();
                }
            }
        }

        /// <summary>
        /// 每次请求初始化数据(含PostBack)
        /// </summary>
        private void InitDataDynamicBefore()
        {

        }

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_autoLoginAuthentication = null;
            }
        }

        protected AutoLoginAuthentication m_autoLoginAuthentication = null;
        protected AutoLoginAuthentication AutoLoginAuthentication
        {
            get
            {
                if (m_autoLoginAuthentication == null)
                {
                    m_autoLoginAuthentication = new AutoLoginAuthentication();
                    if (LastID != 0)
                    {
                        m_autoLoginAuthentication.GetProperties(LastID);
                    }
                }
                return m_autoLoginAuthentication;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        /// <summary>
        /// 查找request里的参数(QueryString).
        /// </summary>
        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (AutoLoginAuthentication.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }

        /// <summary>
        /// 初次请求初始化数据(不含PostBack)
        /// </summary>
        protected void InitDataSelect()
        {
            this.txt_ipAddress.Text = AutoLoginAuthentication.IpAddress;

            this.BindDDLEnterprise();
            this.ddl_enterprise.SelectedValue = AutoLoginAuthentication.EnterpriseId.ToString();

            this.BindDDLUser();
            this.ddl_user.SelectedValue = AutoLoginAuthentication.UserId.ToString();

            this.SetStatus();
        }

        private void BindDDLUser()
        {
            int enterpriseId = Int32.Parse(this.ddl_enterprise.SelectedValue);
            List<EnterpriseUser> userList = new EnterpriseUser().GetEnterpriseUserByEnterpriseId(enterpriseId);

            CommonControlInitial.InitialUserList(ddl_user, userList);
        }
        protected void BindDDLEnterprise()
        {
            CommonControlInitial.InitialEnterpriseList(ddl_enterprise, new Enterprise().GetAll());
        }

        protected void ValidateDataBefore()
        {
            if (this.ddl_enterprise.SelectedValue == "0")
            {
                throw new BYFW.BOL.CustomException("请选择企业！");
            }
        }
        protected void ValidateDataAfter()
        {

        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            AutoLoginAuthentication.EnterpriseId = Int32.Parse(this.ddl_enterprise.SelectedValue);
            AutoLoginAuthentication.UserId = Int32.Parse(this.ddl_user.SelectedValue);
            AutoLoginAuthentication.IpAddress = this.txt_ipAddress.Text.Replace('；', ';');
            this.ValidateDataAfter();
        }

        protected void Add()
        {
            this.SetData();
            AutoLoginAuthentication.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            this.SetData();
            AutoLoginAuthentication.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void ddl_enterprise_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindDDLUser();
        }
    }
}